package controller;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import logic.*;

/**
 * This is the Top Controller class.
 * this class makes it possible to set several aspects of the simulation.
 * other than the initContoller this one stands at the top op the screen
 * @author Vossen en Konijnen group 2
 *
 */
public class TopController extends AbstractController implements ActionListener 
{

	private JMenuBar menuBar;
	private JMenuItem setFox;
	private JMenuItem setRabbit;
	private JMenuItem setDuck;
	private JMenuItem setHunter;
	private JMenuItem setSpawnrate;
	private JMenuItem setGrass;
	private JFrame setframe;
	public TopController(Simulator simulator)
	{
		super(simulator);
		menuBar = new JMenuBar();
		JMenu menu = new JMenu("Set");
		menuBar.add(menu);
		setFox = new JMenuItem("Set Fox");
		setFox.addActionListener(this);
		menu.add(setFox);
		setRabbit = new JMenuItem("Set Rabbit");
		setRabbit.addActionListener(this);
		menu.add(setRabbit);
		setDuck = new JMenuItem("Set Duck");
		setDuck.addActionListener(this);
		menu.add(setDuck);
		setHunter = new JMenuItem("Set Hunter");
		setHunter.addActionListener(this);
		menu.add(setHunter);
		setGrass = new JMenuItem("Set Grass");
		setGrass.addActionListener(this);
		menu.add(setGrass);
		setSpawnrate= new JMenuItem("Set Spawn rate");
		setSpawnrate.addActionListener(this);
		menu.add(setSpawnrate);
	}
	
	/**
	 * get the MenuBar.
	 * @return menuBar
	 */
	public JMenuBar getMenu()
	{
		return menuBar;
	}
	
	/**
	 * set the characteristics of the fox.
	 */
	public void setFox()
	{
		setframe = new JFrame("setFox");
		Container content = setframe.getContentPane();
		content.setLayout(new BorderLayout());
		JLabel info = new JLabel(Fox.getStatic().substring(0,60));
		content.add(info, BorderLayout.NORTH);
		info = new JLabel(Fox.getStatic().substring(60,Fox.getStatic().length()));
		content.add(info, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel label = new JLabel("Breeding age ");
	    panel.add(label);
	    final JTextField breed = new JTextField(0);
	    panel.add(breed);
	    
	    label = new JLabel("Maximum age ");
	    panel.add(label);
	    final JTextField age = new JTextField(0);
	    panel.add(age);
	    
	    label =  new JLabel("Breeding Probability");
	    panel.add(label);
	    final JTextField breedChance = new JTextField(0);
	    panel.add(breedChance);
	    
	    
	    label = new JLabel("Maximum Amount of new foxes");
	    panel.add(label);
	    final JTextField spawn = new JTextField(0);
	    panel.add(spawn);
	    
	    label = new JLabel("Rabbit food value");
	    panel.add(label);
	    final JTextField RFood = new JTextField(0);
	    panel.add(RFood);
	    
	    label = new JLabel("Duck food value");
	    panel.add(label);
	    final JTextField DFood = new JTextField(0);
	    panel.add(DFood);
	    
	    JButton button = new JButton("Set");
	    button.addActionListener(new ActionListener() 
            {
                   public void actionPerformed(ActionEvent e)
                   {
                	   try{
                	   Fox.setStatic(
                			   intParser(breed.getText()),
                			   intParser(age.getText()),
                			   Double.parseDouble(breedChance.getText()),
                			   intParser(spawn.getText()),
                			   intParser(RFood.getText()),
                			   intParser(DFood.getText())
                	   );
                	   setframe.dispose();
                	   }
                	   catch(Exception ex)
                	   {
                		   JOptionPane.showMessageDialog(null, "Velden fout ingevuld");
                	   }
                   }
            });
	    panel.add(button);
	    content.add(panel, BorderLayout.SOUTH);
	    setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setframe.pack();
		setframe.setVisible(true);
	}
	
	/**
	 * set the characteristics of the rabbit.	 
	 */
	public void setRabbit()
	{
		setframe = new JFrame("setRabbit");
		Container content = setframe.getContentPane();
		content.setLayout(new BorderLayout());
		JLabel info = new JLabel(Rabbit.getStatic().substring(0,58));
		content.add(info, BorderLayout.NORTH);
		info = new JLabel(Rabbit.getStatic().substring(58,Rabbit.getStatic().length()));
		content.add(info, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel label = new JLabel("Breeding age ");
	    panel.add(label);
	    final JTextField breed = new JTextField(0);
	    panel.add(breed);
	    
	    label = new JLabel("Maximum age ");
	    panel.add(label);
	    final JTextField age = new JTextField(0);
	    panel.add(age);
	    
	    label =  new JLabel("Breeding probability");
	    panel.add(label);
	    final JTextField breedChance = new JTextField(0);
	    panel.add(breedChance);
	    
	    
	    label = new JLabel("Maximum Amount of new rabbits");
	    panel.add(label);
	    final JTextField spawn = new JTextField(0);
	    panel.add(spawn);
	    
	    label = new JLabel("Grass food value");
	    panel.add(label);
	    final JTextField GFood = new JTextField(0);
	    panel.add(GFood);
	    
	    label = new JLabel("Disease On/Off");
	    panel.add(label);
	    final JCheckBox disease = new JCheckBox();
	    panel.add(disease);
	    
	    JButton button = new JButton("Set");
	    button.addActionListener(new ActionListener() 
            {
                   public void actionPerformed(ActionEvent e)
                   {
                	   try{
                	   Rabbit.setStatic(
                			   intParser(breed.getText()),
                			   intParser(age.getText()),
                			   Double.parseDouble(breedChance.getText()),
                			   intParser(spawn.getText()),
                			   intParser(GFood.getText()),
                			   disease.isSelected()
                	   );
                	   disease.isSelected();
                	   setframe.dispose();
                	   }
                	   catch(Exception ex)
                	   {
                		   JOptionPane.showMessageDialog(null, "Velden fout ingevuld");
                	   }
                   }
            });
	    panel.add(button);
	    content.add(panel, BorderLayout.SOUTH);
	    setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setframe.pack();
		setframe.setVisible(true);
	}
	
	/**
	 * set the characteristics of the duck.
	 */
	public void setDuck()
	{
		setframe = new JFrame("setDuck");
		Container content = setframe.getContentPane();
		content.setLayout(new BorderLayout());
		JLabel info = new JLabel(Duck.getStatic().substring(0,57));
		content.add(info, BorderLayout.NORTH);
		info = new JLabel(Duck.getStatic().substring(57,Duck.getStatic().length()));
		content.add(info, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel label = new JLabel("Breeding age ");
	    panel.add(label);
	    final JTextField breed = new JTextField(0);
	    panel.add(breed);
	    
	    label = new JLabel("Maximum age ");
	    panel.add(label);
	    final JTextField age = new JTextField(0);
	    panel.add(age);
	    
	    label =  new JLabel("Breeding probability");
	    panel.add(label);
	    final JTextField breedChance = new JTextField(0);
	    panel.add(breedChance);
	    
	    
	    label = new JLabel("Maximum amount of new ducks");
	    panel.add(label);
	    final JTextField spawn = new JTextField(0);
	    panel.add(spawn);
	    
	    label = new JLabel("Grass food value");
	    panel.add(label);
	    final JTextField GFood = new JTextField(0);
	    panel.add(GFood);
	    
	    JButton button = new JButton("Set");
	    button.addActionListener(new ActionListener() 
            {
                   public void actionPerformed(ActionEvent e)
                   {
                	   try{
                	   Duck.setStatic(
                			   intParser(breed.getText()),
                			   intParser(age.getText()),
                			   Double.parseDouble(breedChance.getText()),
                			   intParser(spawn.getText()),
                			   intParser(GFood.getText())
                	   );
                	   setframe.dispose();
                	   }
                	   catch(Exception ex)
                	   {
                		   JOptionPane.showMessageDialog(null, "Velden fout ingevuld");
                	   }
                   }
            });
	    panel.add(button);
	    content.add(panel, BorderLayout.SOUTH);
	    setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setframe.pack();
		setframe.setVisible(true);
	}
	
	/**
	 * set the characteristics of the patches of grass.
	 */
	public void setGrass()
	{
		setframe = new JFrame("setGrass");
		Container content = setframe.getContentPane();
		content.setLayout(new BorderLayout());
		JLabel info = new JLabel(Grass.getStatic().substring(0,57));
		content.add(info, BorderLayout.NORTH);
		info = new JLabel(Grass.getStatic().substring(57,Grass.getStatic().length()));
		content.add(info, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel label = new JLabel("Growing age ");
	    panel.add(label);
	    final JTextField grow = new JTextField(0);
	    panel.add(grow);
	    
	    label = new JLabel("Maximum age ");
	    panel.add(label);
	    final JTextField age = new JTextField(0);
	    panel.add(age);
	    
	    label =  new JLabel("Growing probability");
	    panel.add(label);
	    final JTextField growChance = new JTextField(0);
	    panel.add(growChance);
	    
	    
	    label = new JLabel("Maximum amount of new seeds");
	    panel.add(label);
	    final JTextField spawn = new JTextField(0);
	    panel.add(spawn);
	    
	    JButton button = new JButton("Set");
	    button.addActionListener(new ActionListener() 
            {
                   public void actionPerformed(ActionEvent e)
                   {
                	   try{
                	   Grass.setStatic(
                			   intParser(grow.getText()),
                			   intParser(age.getText()),
                			   Double.parseDouble(grow.getText()),
                			   intParser(spawn.getText())
                	   );
                	   setframe.dispose();
                	   }
                	   catch(Exception ex)
                	   {
                		   JOptionPane.showMessageDialog(null, "Velden fout ingevuld");
                	   }
                   }
            });
	    panel.add(button);
	    content.add(panel, BorderLayout.SOUTH);
	    setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setframe.pack();
		setframe.setVisible(true);
	}
	
	/**
	 * set the spawnrate of the actors.
	 */
	public void setSpawn()
	{
		setframe = new JFrame("setSpawnrate");
		Container content = setframe.getContentPane();
		content.setLayout(new BorderLayout());
		JLabel info = new JLabel(Simulator.getStatic().substring(0,58));
		content.add(info, BorderLayout.NORTH);
		info = new JLabel(Simulator.getStatic().substring(58,Simulator.getStatic().length()));
		content.add(info, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel label = new JLabel("Fox chance ");
	    panel.add(label);
	    final JTextField Fspawn = new JTextField(0);
	    panel.add(Fspawn);
	    
	    label = new JLabel("Rabbit chance ");
	    panel.add(label);
	    final JTextField Rspawn = new JTextField(0);
	    panel.add(Rspawn);
	    
	    label =  new JLabel("Duck chance ");
	    panel.add(label);
	    final JTextField Dspawn = new JTextField(0);
	    panel.add(Dspawn);
	    
	    
	    label = new JLabel("Hunter chance ");
	    panel.add(label);
	    final JTextField Hspawn = new JTextField(0);
	    panel.add(Hspawn);
	    
	    label = new JLabel("Grass chance ");
	    panel.add(label);
	    final JTextField Gspawn = new JTextField(0);
	    panel.add(Gspawn);
	    
	    JButton button = new JButton("Set");
	    button.addActionListener(new ActionListener() 
            {
                   public void actionPerformed(ActionEvent e)
                   {
                	   try{
                	   simulator.setStatic(
                			   Double.parseDouble(Fspawn.getText()),
                			   Double.parseDouble(Rspawn.getText()),
                			   Double.parseDouble(Dspawn.getText()),
                			   Double.parseDouble(Hspawn.getText()),
                			   Double.parseDouble(Gspawn.getText())
                	   );
                	   setframe.dispose();
                	   }
                	   catch(Exception ex)
                	   {
                		   JOptionPane.showMessageDialog(null, "Velden fout ingevuld");
                	   }
                   }
            });
	    panel.add(button);
	    content.add(panel, BorderLayout.SOUTH);
	    setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setframe.pack();
		setframe.setVisible(true);
	}
	
	/**
	 * set the characteristics of the hunter.
	 */
	public void setHunter()
	{
		setframe = new JFrame("setHunter");
		Container content = setframe.getContentPane();
		content.setLayout(new BorderLayout());
		JLabel info = new JLabel("Trap chance: 1:"+ Hunter.getTrapChance());
		content.add(info, BorderLayout.NORTH);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel label = new JLabel("Trap Chance");
	    panel.add(label);
	    final JTextField tChance = new JTextField(0);
	    panel.add(tChance);
	    
	
	    
	    JButton button = new JButton("Set");
	    button.addActionListener(new ActionListener() 
            {
                   public void actionPerformed(ActionEvent e)
                   {
                	   try{
                	   Hunter.setTrapChance(
                			 intParser(tChance.getText())
                	   );
                	   setframe.dispose();
                	   }
                	   catch(Exception ex)
                	   {
                		   JOptionPane.showMessageDialog(null, "Veld fout ingevuld");
                	   }
                   }
            });
	    panel.add(button);
	    content.add(panel, BorderLayout.SOUTH);
	    setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setframe.pack();
		setframe.setVisible(true);
	}
	
	/**
	 * return the string into int.
	 * @param s
	 * @return int
	 */
	public int intParser(String s)
	{
		return Integer.parseInt(s);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == setFox)
		{
			setFox();
		}
		if(e.getSource() == setRabbit)
		{
			setRabbit();
		}
		if(e.getSource() == setDuck)
		{
			setDuck();
		}
		if(e.getSource() == setHunter)
		{
			setHunter();
		}
		if(e.getSource() == setGrass)
		{
			setGrass();
		}
	    if(e.getSource() == setSpawnrate)
	    {
	    	setSpawn();
	    }
	}
	
}